﻿using blackhouse.JavaScript.JQuery;
using blackhouse.Locations;
using blackhouse.WebNemo.Business.Groups;
using mojoPortal.Web;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.Web;

namespace blackhouse.StareKino.Web.Contact {
    public partial class ContactModule : SiteModuleControl {

        #region Fields

        private GroupRecord contactPageRecord;
        private Location hotelLocation;
        private int placeCounts;

        #endregion

        #region Methods

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.contactPageRecord = this.GetContactPageRecord(this.ModuleId);

            if (blackhouse.WebNemo.Business.Factory.Instance().GetCurrentUser().HasSufficientPermissions(false))
                this.lnkNemoEditContactPage.RecordId = this.contactPageRecord.RecordId;
            else
                this.phEditActions.Visible = false;

            this.PopulateRecordLabel(this.ltlSubtitle, "subtitle");
            this.PopulateRecordLabel(this.ltlHotelName, "hotel_name");
            this.PopulateRecordLabel(this.ltlHotelSubtitle, "hotel_subtitle");
            this.PopulateRecordLabel(this.ltlHotelPostCode, "hotel_postcode");
            this.PopulateRecordLabel(this.ltlHotelCity, "hotel_city");
            this.PopulateRecordLabel(this.ltlHotelStreet, "hotel_street");
            if (this.contactPageRecord.IsNotNull("receipt_email")) {
                string email = this.contactPageRecord.GetString("receipt_email");
                string format = "{0}";
                if (this.lnkReceipt.Text.Contains("{0}"))
                    format = this.lnkReceipt.Text;
                this.lnkReceipt.Text = String.Format(format, email);
                this.lnkReceipt.NavigateUrl = "mailto:" + email;
            } else
                this.lnkReceipt.Visible = false;
            this.PopulateRecordLabel(this.ltlPhone, "receipt_phone");
            this.PopulateRecordLabel(this.ltlCellPhone, "receipt_cellphone");
            this.PopulateRecordLabel(this.ltlInfoPhone, "receipt_infophone");
            this.hotelLocation = this.contactPageRecord.GetCustomValue<Location>("hotel_location");
            if (this.hotelLocation != null)
                this.hiddenHotelLocation.Value = hotelLocation.Latitude.ToString(CultureInfo.GetCultureInfo("en-US")) + "," + hotelLocation.Longitude.ToString(CultureInfo.GetCultureInfo("en-US"));
            ContactPlaces places = ContactPlace.GetPlaces();
            this.rptCategories.DataSource = places.GetCategories();
            this.rptCategories.DataBind();

            this.placeCounts = places.Count();
            this.rptPlaces.DataSource = places;
            this.rptPlaces.DataBind();

            this.Page.ClientScript.RegisterClientScriptInclude("google-maps", "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false");
            JQueryLibraries jql = new JQueryLibraries();
            jql.IncludeFileBasedOnJQuery("blackhouse.StareKino.Web.Scripts.contact.js,blackhouse.StareKino.Web", true);
        }

        private void PopulateRecordLabel(ITextControl control, string columnName) {
            if (this.contactPageRecord.IsNotNull(columnName)) {
                string format = "{0}";
                if (control.Text.Contains("{0}"))
                    format = control.Text;
                control.Text = String.Format(format, this.contactPageRecord.GetString(columnName));
            } else
                (control as Control).Visible = false;
        }

        private GroupRecord GetContactPageRecord(int moduleId) {
            IGroup group = blackhouse.WebNemo.Business.Factory.Instance().GetGroup("CONTACT_PAGE");
            GroupRecords gr = new GroupRecords(group);
            GroupRecordQuery recordQuery = new GroupRecordQuery(group);
            recordQuery.AddWhereClause("module_id", moduleId);
            GroupRecord[] searchRecords = gr.GetRecords(recordQuery);
            if (searchRecords.Length == 0) {
                GroupRecord record = new NewGroupRecord(group);
                record.SetValue("module_id", moduleId);
                record.SetValue("subtitle", "Można zadzwonić do nas w każdej chwili");
                record.SetValue("hotel_name", "Cinema Hotel");
                record.SetValue("hotel_subtitle", "Stylowe apartamenty");
                record.SetValue("hotel_postcode", "90-644");
                record.SetValue("hotel_city", "Łódź");
                record.SetValue("hotel_street", "Piotrkowska 120");
                record.SetValue("receipt_email", "biuro@cinemahotel.pl");
                record.SetValue("receipt_phone", "+48 42 203 58 81");
                record.SetValue("receipt_cellphone", "+48 880 00 08 01");
                record.SetValue("receipt_infophone", "880 000 801");
                record.Save();
                return record;
            } else
                return searchRecords.First();
        }

        protected void rptPlaces_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            switch (e.Item.ItemType) {
                case ListItemType.AlternatingItem:
                case ListItemType.Item:
                    ContactPlace place = e.Item.DataItem as ContactPlace;
                    Literal ltlDistanceValue = e.Item.FindControl("ltlDistanceValue") as Literal;
                    Literal ltlDistanceTime = e.Item.FindControl("ltlDistanceTime") as Literal;
                    Literal ltlTotalCount = e.Item.FindControl("ltlTotalCount") as Literal;
                    if (place != null) {
                        Distance distance = null;
                        if (this.hotelLocation != null) distance = place.GetDistance(this.hotelLocation);
                        place.GetDistance(this.hotelLocation);
                        if (ltlDistanceValue != null) {
                            if (distance != null) {
                                ltlDistanceValue.Text = distance.ToString();
                            } else {
                                ltlDistanceValue.Visible = false;
                            }
                        }
                        if (ltlDistanceTime != null) {
                            if (distance != null) {
                                ltlDistanceTime.FormatOnTextFormat(distance.GetSpeedInMin(7), "{0:F0} min.");
                            } else
                                ltlDistanceTime.Visible = false;
                        }
                        if (ltlTotalCount != null)
                            ltlTotalCount.Text = this.placeCounts.ToString();
                    } else
                        e.Item.Visible = false;
                    break;
            }
        }

        #endregion

    }
}